package com.shujia.java.day14.MapToListDemo2;

import com.shujia.java.day14.ListToMapDemo1.Student;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;

/**
 * map->list
 */
public class MapToListTest1 {
    public static void main(String[] args) {
        //创建集合
        HashMap<Integer, String> map = new HashMap<Integer, String>();
        //不使用Student 模拟学生数据
        map.put(10001,"10001,张三,18,男");
        map.put(10002,"10002,李四,18,男");
        map.put(10003,"10003,王五,18,男");
        map.put(10004,"10004,赵六,18,男");

        //list
        ArrayList<Student> list = new ArrayList<>();

        Student student = null;

        Set<Integer> integers = map.keySet();

        for (Integer i:integers){
            //输出key和Value
            System.out.print("这是key:"+i+"-------------");
            String str=map.get(i);
            //处理value 切分
            String[] split = str.split(",");
            //外面Student给Null 在循环里面new对象
            student=new Student();
            //给student赋值
            student.setId(Integer.parseInt(split[0]));
            student.setName(split[1]);
            student.setAge(Integer.parseInt(split[2]));
            student.setSex(split[3]);

            list.add(student);
//            for (String s:split) {
//                System.out.print(s+"  ");
//            }
            System.out.println();
        }
        //遍历list 查看是否正确
        for (Student s:list){
            System.out.println(s.getName());
        }
    }
}
